package com.example.huainanlive.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.huainanlive.R
import com.example.huainanlive.adapter.LivePlayAdapter
import com.example.huainanlive.databinding.FragmentLivePlayBinding

class LivePlayFragment :BaseBindingViewFragment<FragmentLivePlayBinding>(R.layout.fragment_live_play)  {

    private lateinit var huiGuLiveViewModel: HuiGuLiveViewModel
    private lateinit var livePlayAdapter: LivePlayAdapter
    private var mWsViewModel: WsViewModel? = null

    override fun initBinding(layoutInflater: LayoutInflater): FragmentLivePlayBinding {
        return FragmentLivePlayBinding.inflate(layoutInflater)
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        huiGuLiveViewModel = ViewModelProvider(
            requireActivity()
        )[HuiGuLiveViewModel::class.java]

        mWsViewModel = ViewModelProvider(
            requireActivity(),
            WsViewModel.WsViewModelModelFactory()
        )[WsViewModel::class.java]

        //添加主持人发言
        mWsViewModel?.addComment?.observe(viewLifecycleOwner){
            it.t?.let { data ->
                livePlayAdapter.addData(data)
                binding.rvLivePlay.smoothScrollToPosition(livePlayAdapter.data.size-1)
            }
        }
        //撤销主持人发言
        mWsViewModel?.cancelComment?.observe(viewLifecycleOwner){ commentId->

            livePlayAdapter.data.forEach {
                if (it.baseId == commentId.t.toString()){
                    livePlayAdapter.remove(it)
                }
            }

        }


        livePlayAdapter= LivePlayAdapter()
        binding.rvLivePlay.layoutManager = LinearLayoutManager(activity)
        binding.rvLivePlay.adapter = livePlayAdapter

        //主持人信息
        huiGuLiveViewModel.getCommentItemBean.observe(viewLifecycleOwner){
            livePlayAdapter.setList(it)
            binding.rvLivePlay.smoothScrollToPosition(it.size-1)
        }

//        livePlayAdapter.setOnItemLongClickListener { adapter, view, position ->
//
//        }

    }



}